#NPM run not recognizing astro dev script

19 messages · Page 1 of 1 (latest)

jovial island
#

I created an astro website from the astro template, but whenever I try to use npm run dev, I get this error:

33 timing command:run Completed in 10ms
34 verbose stack Error: Missing script: "astro dev"
34 verbose stack
34 verbose stack To see a list of scripts, run:
34 verbose stack   npm run
34 verbose stack     at RunScript.run (/usr/local/share/nvm/versions/node/v20.11.0/lib/node_modules/npm/lib/commands/run-script.js:94:13)
34 verbose stack     at async module.exports (/usr/local/share/nvm/versions/node/v20.11.0/lib/node_modules/npm/lib/cli-entry.js:61:5)
35 verbose cwd /workspaces/project-pitch-magic-queries-clone
36 verbose Linux 6.2.0-1018-azure
37 verbose node v20.11.0
38 verbose npm  v10.2.4
39 error Missing script: "astro dev"
39 error
39 error To see a list of scripts, run:
39 error   npm run
40 verbose exit 1
41 timing npm Completed in 96ms
42 verbose code 1
43 error A complete log of this run can be found in: /home/codespace/.npm/_logs/2024-01-26T02_05_35_342Z-debug-0.log```

I think NPM is failing to find my scripts, but I don't know how to fix the issue.
storm warrenBOT
#

Hi @jovial island. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error message you're seeing suggests that the astro dev script is not defined in your package.json file. When you create a new Astro project, the astro dev command should be automatically added to the scripts section of your package.json file.

Here's an example of what the scripts section in your package.json file should look like:

"scripts": {
  "dev": "astro dev",
  "start": "astro dev",
  "build": "astro build",
  "preview": "astro preview"
}

If your package.json file doesn't include these scripts, you can add them manually. After adding the scripts, you should be able to run npm run dev to start the Astro development server.

If you're still having trouble, it might be helpful to review the Astro CLI documentation (https://docs.astro.build/en/reference/cli-reference#packagejson-scripts) or check for similar issues in the Astro GitHub repository (https://github.com/withastro/astro/issues).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@jovial island kapa.ai is still learning and improving, please let us know how it did by reacting below

jovial island
#

@storm warren I already have the package.json file, with the astro scripts already inside. However, I noticed that I have 2 different files named package.json. One of the package.json files is located in the topmost project folder, and the other package.json file is located within the astro project directory. Could this be the problem?

storm warrenBOT
#

Let me check the knowledge base for you. One moment! :books:

#

Yes, having two package.json files could potentially cause confusion. The package.json file that's relevant to your Astro project should be located in the root directory of the Astro project itself.

When you run npm run dev (or any other npm script), npm looks for the package.json file in the current directory. If you're running the command from the topmost project folder, npm might be looking at the wrong package.json file.

To fix this issue, you should navigate to the Astro project directory before running the command. You can do this using the cd command in your terminal. For example, if your Astro project is located in a directory named astro-project, you would navigate to it like this:

cd astro-project

Once you're in the correct directory, you should be able to run npm run dev without any issues.

If you're working in a monorepo setup (a repository that contains multiple projects), you might need to add project-specific dependencies to each project's own package.json file, as mentioned in the Astro documentation (https://docs.astro.build/en/guides/troubleshooting#adding-dependencies-to-astro-in-a-monorepo).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@jovial island kapa.ai is still learning and improving, please let us know how it did by reacting below

jovial island
#

@storm warren I changed my working directory and that allowed NPM to see the astro scripts. However, when I try running any of the astro scripts, I get this in the console:


> [email protected] dev
> astro dev

sh: 1: astro: not found
@Emblin13 ➜ /workspaces/project-pitch-magic-queries-clone/magic-queries (main) $ npm run build

> [email protected] build
> astro build

sh: 1: astro: not found```
storm warrenBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The error message sh: 1: astro: not found suggests that the astro package is not installed in your project's node_modules directory, or it's not accessible from the command line.

Here are a few things you can try to resolve this issue:

  1. Install Astro locally in your project: Run npm install astro in your project directory. This will install Astro as a local dependency in your project.

  2. Check your node_modules/.bin directory: The astro command should be located in the node_modules/.bin directory of your project. If it's not there, something might have gone wrong during the installation process.

  3. Reinstall your node modules: Delete your node_modules directory and your package-lock.json file (or yarn.lock if you're using Yarn), then run npm install to reinstall your dependencies.

  4. Check your PATH variable: If you installed Astro globally and it's still not found, it might be that the location of global npm packages is not included in your PATH environment variable. You can check your PATH variable by running echo $PATH in your terminal.

If you're still having trouble, it might be helpful to review the Astro setup guide (https://docs.astro.build/en/tutorial/1-setup/2#_top) or check for similar issues in the Astro GitHub repository (https://github.com/withastro/astro/issues).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@jovial island kapa.ai is still learning and improving, please let us know how it did by reacting below

jovial island
#

@storm warren In a proper Astro website, should the node_modules folder be located inside or outside of the Astro project folder?

storm warrenBOT
#

Let me check the knowledge base for you. One moment! :books:

#

The node_modules folder should be located in the root directory of your Astro project. This is where all the dependencies of your project, including Astro itself, are installed when you run npm install or yarn install.

Here's an example of what your project directory should look like after setting up Astro:

  • node_modules/
  • public/
    • robots.txt
  • src/
    • pages/
      • index.astro
    • env.d.ts
  • astro.config.mjs
  • package-lock.json or yarn.lock, pnpm-lock.yaml, etc.
  • package.json
  • tsconfig.json

As you can see, the node_modules folder is at the same level as the public and src directories, not inside them. This is the standard structure for JavaScript and TypeScript projects, not just Astro.

For more information, you can refer to the Astro documentation (https://docs.astro.build/en/install/manual#7-next-steps).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@jovial island kapa.ai is still learning and improving, please let us know how it did by reacting below

jovial island
#

@storm warren My issue was that I had the node_modules folder outside of my Astro project. I reinstalled Node inside the project folder, and the scripts began working as intended. You were very helpful, thank you Kapa

storm warrenBOT
#

Let me check the knowledge base for you. One moment! :books:

#

I'm glad to hear that your issue has been resolved! It's important to have the node_modules folder in the correct location, as it contains all the dependencies your project needs to run properly.

Remember, when you're setting up a new Astro project, you should run npm install or yarn install inside the project directory. This will create a node_modules folder in the right place and install all the necessary dependencies.

If you encounter any more issues or have further questions about Astro, don't hesitate to ask. Happy coding!

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: