I have never used npm
before so I may not be asking the question how it should be asked but I could not find this information anywhere. Let's say I am interested in using this module. The author asks me to install the module which I do by typing the following in command line.
我以前从未使用过npm所以我可能不会问这个问题应该如何被问到但是我无法在任何地方找到这些信息。假设我对使用此模块感兴趣。作者要求我通过在命令行中键入以下内容来安装我所做的模块。
npm install --save critical
I am lost after that. The author writes a bunch of lines but I have no idea where to put them.
那之后我迷路了。作者写了一堆行,但我不知道把它们放在哪里。
var critical = require('critical');
and
和
critical.generate({
inline: true,
base: 'test/',
src: 'index.html',
dest: 'index-critical.html',
width: 1300,
height: 900
});
Where do I put this code and how do I run it. I tried creating a file called criticaldemo.js
and put all this code there but running it through command line did nothing. TO run it through command line I used the following command:
我在哪里放这个代码,我该如何运行它。我尝试创建一个名为criticaldemo.js的文件并将所有这些代码放在那里,但是通过命令行运行它什么也没做。要通过命令行运行它我使用以下命令:
$ criticalfile.js
Can anyone please help me understand how to use npm modules?
谁能帮助我了解如何使用npm模块?
1 个解决方案
#1
1
Let's assume your project directory structure looks like:
我们假设你的项目目录结构如下:
project
criticaldemo.js
You should navigate to project
and run
您应该导航到项目并运行
npm init
This will create package.json
inside your project. You can just press enter for each question to use the default values.
这将在您的项目中创建package.json。您只需按每个问题的回车键即可使用默认值。
If you prefer, you can also manually create package.json
based on the requirements.
如果您愿意,还可以根据要求手动创建package.json。
Here's how it should look, as a bare minimum (must have at least a name
and version
):
这是它应该看起来的样子,作为最低限度(必须至少有一个名称和版本):
{
"name": "my-cool-project",
"version": "1.0.0"
}
Either way, at this point your directory should look like:
无论哪种方式,此时您的目录应如下所示:
project
criticaldemo.js
package.json
Next, you run:
接下来,您运行:
npm install --save critical
This will create a directory node_modules
with your package inside of it:
这将创建一个目录node_modules,其中包含您的包:
Directory structure:
目录结构:
project
criticaldemo.js
package.json
node_modules
Now inside your criticaldemo.js
, you put the code as the package author said:
现在在你的criticaldemo.js中,你把代码作为包作者说:
var critical = require('critical'); // this loads the package from node_modules/critical
critical.generate({
inline: true,
base: 'test/',
src: 'index.html',
dest: 'index-critical.html',
width: 1300,
height: 900
});
Finally you run the program using node:
最后,使用节点运行程序:
node criticaldemo.js
#1
1
Let's assume your project directory structure looks like:
我们假设你的项目目录结构如下:
project
criticaldemo.js
You should navigate to project
and run
您应该导航到项目并运行
npm init
This will create package.json
inside your project. You can just press enter for each question to use the default values.
这将在您的项目中创建package.json。您只需按每个问题的回车键即可使用默认值。
If you prefer, you can also manually create package.json
based on the requirements.
如果您愿意,还可以根据要求手动创建package.json。
Here's how it should look, as a bare minimum (must have at least a name
and version
):
这是它应该看起来的样子,作为最低限度(必须至少有一个名称和版本):
{
"name": "my-cool-project",
"version": "1.0.0"
}
Either way, at this point your directory should look like:
无论哪种方式,此时您的目录应如下所示:
project
criticaldemo.js
package.json
Next, you run:
接下来,您运行:
npm install --save critical
This will create a directory node_modules
with your package inside of it:
这将创建一个目录node_modules,其中包含您的包:
Directory structure:
目录结构:
project
criticaldemo.js
package.json
node_modules
Now inside your criticaldemo.js
, you put the code as the package author said:
现在在你的criticaldemo.js中,你把代码作为包作者说:
var critical = require('critical'); // this loads the package from node_modules/critical
critical.generate({
inline: true,
base: 'test/',
src: 'index.html',
dest: 'index-critical.html',
width: 1300,
height: 900
});
Finally you run the program using node:
最后,使用节点运行程序:
node criticaldemo.js